let arr = [ { belongid: '63',
courseid: '95',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
startstamp: '00:00',
currenttime: '2018-03-07 17:32:47',
action: 'play' },
{ belongid: '63',
courseid: '95',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
stopstamp: '00:01',
courseduration: '02:15',
currenttime: '2018-03-07 17:32:48',
action: 'pause' },
{ belongid: '63',
courseid: '96',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
startstamp: '00:00',
currenttime: '2018-03-07 17:32:48',
action: 'play' },
{ belongid: '63',
courseid: '96',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
stopstamp: '00:00',
courseduration: '02:58',
currenttime: '2018-03-07 17:32:49',
action: 'pause' },
{ belongid: '63',
courseid: '97',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
startstamp: '00:00',
currenttime: '2018-03-07 17:32:49',
action: 'play' },
{ belongid: '63',
courseid: '97',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
stopstamp: '00:01',
courseduration: '02:35',
currenttime: '2018-03-07 17:32:50',
action: 'pause' },
{ belongid: '63',
courseid: '97',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
startstamp: '00:01',
currenttime: '2018-03-07 17:32:50',
action: 'play' },
{ belongid: '63',
courseid: '97',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
stopstamp: '00:02',
courseduration: '02:35',
currenttime: '2018-03-07 17:32:51',
action: 'pause' },
{ belongid: '63',
courseid: '96',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
startstamp: '00:00',
currenttime: '2018-03-07 17:32:51',
action: 'play' },
{ belongid: '63',
courseid: '96',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
stopstamp: '00:01',
courseduration: '02:58',
currenttime: '2018-03-07 17:32:52',
action: 'pause' },
{ belongid: '63',
courseid: '99',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
startstamp: '00:00',
currenttime: '2018-03-07 17:32:53',
action: 'play' },
{ belongid: '63',
courseid: '99',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
stopstamp: '00:01',
courseduration: '03:20',
currenttime: '2018-03-07 17:32:54',
action: 'pause' },
{ belongid: '63',
courseid: '99',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
startstamp: '00:01',
currenttime: '2018-03-07 17:32:55',
action: 'play' },
{ belongid: '63',
courseid: '99',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
stopstamp: '00:02',
courseduration: '03:20',
currenttime: '2018-03-07 17:32:56',
action: 'pause' },
{ belongid: '63',
courseid: '100',
coursetype: 1,
userid: '200100280',
appid: 'wxe4546402d8eaabe2',
startstamp: '00:00',
currenttime: '2018-03-07 17:32:56',
action: 'play' } ];












formatData(arr);
function formatData(arr) {
    const result = [];      //最终结果
    let currentObj  = {}; //当前对象
    let currentIndex = 0;       //当前结果索引

    let coursetype = null;   //当前coursetype
    let belongid = null;     //当前belongid
    let courseid = null;     //当前courseid

    arr.map( (item,index) => {
        console.log(index,item);
        if ( index === 0 ) {  
            recodePlayData(item,currentObj);
            coursetype = item.coursetype;
            belongid = item.belongid;
            courseid = item.courseid;
            return false;
        }

        if ( item.coursetype !== coursetype ) {
            recodePauseData(arr[index-1],currentObj);
            result.push(currentObj);

            recodePlayData(item,currentObj);
            coursetype = item.coursetype;
            belongid = item.belongid;
            courseid = item.courseid;
            return false;
        }

        if ( item.belongid !== belongid ) {
            recodePauseData(arr[index-1],currentObj);
            result.push(currentObj);

            recodePlayData(item,currentObj);
            coursetype = item.coursetype;
            belongid = item.belongid;
            courseid = item.courseid;
            return false;
        }
        
        if ( item.courseid !== courseid ) {
            recodePauseData(arr[index-1],currentObj);
            result.push(currentObj);

            recodePlayData(item,currentObj);
            coursetype = item.coursetype;
            belongid = item.belongid;
            courseid = item.courseid;
            return false;
        }

        if ( item.action === 'pause' ) {
            recodePauseData(item,currentObj);
            result.push(currentObj);
            currentObj = {};
            return false;
        }

        if ( item.action === 'play' && !currentObj.appid ) {
            recodePlayData(item,currentObj);
            coursetype = item.coursetype;
            belongid = item.belongid;
            courseid = item.courseid;
            return false;
        }

        if ( index === arr.length -1 ) {
            recodePauseData(item,currentObj);
        }
    });
    //console.log(result);
    return result;
}

function recodePlayData(item,currentObj) {
    //console.log(item,item.startstamp);
    currentObj.courseid = item.courseid;
    currentObj.belongid = item.belongid;
    currentObj.coursetype = item.coursetype;
    currentObj.userid = item.userid;
    currentObj.starttime = item.currenttime;
    currentObj.startstamp = getSecond(item.startstamp);
    currentObj.appid = item.appid;
}

function recodePauseData(item,currentObj) {
    //console.log(item,item.stopstamp,item.startstamp,!!item.stopstamp);
    currentObj.stopstamp = item.stopstamp ? getSecond(item.stopstamp) : getSecond(item.startstamp);
    currentObj.stoptime = item.currenttime;
    currentObj.playduration =  currentObj.stopstamp - currentObj.startstamp || currentObj.stopstamp || currentObj.stopstamp;
    currentObj.courseduration = getSecond(item.courseduration);
}

function getSecond(time) {
    //console.log(time);
    const timeArr = time.split(':');
    return parseInt(timeArr[0])*60 + parseInt(timeArr[1]);
}

function changeFlag(item,coursetype,belongid,courseid) {
    coursetype = item.coursetype;
    belongid = item.belongid;
    courseid = item.courseid;
}






